package com.lazar.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse res = (HttpServletResponse) servletResponse;

        HttpSession session = req.getSession();

        String path = req.getRequestURI();

        if (path.equals("/usermanage/login.jsp") || path.equals("/usermanage/login")){
            filterChain.doFilter(req,res);
            return;
        }

        if (session.getAttribute("logined") !=null ){
           filterChain.doFilter(req,res);
        }else{
            res.sendRedirect("/usermanage/login.jsp");
        }

//        filterChain.doFilter(req,res);
    }

    @Override
    public void destroy() {

    }
}
